<template>
  <div>
    <div class="header">
      <div class="header-content">
        <div class="header-left">
          <h2 class="title">来首音乐</h2>
          <div class="subtitle">发现更多美好旋律</div>
        </div>
        <div class="search">
          <el-autocomplete
            :prefix-icon="Search"
            placeholder="搜索歌曲、歌手、专辑..."
            v-model="searchWord"
            :fetch-suggestions="getSearchSuggest as any"
            @select="selectSuggest"
            class="search-input"
          >
            <template #suffix>
              <el-icon class="el-input__icon" @click="clearSearchWord" color="#409eff">
                <CircleClose />
              </el-icon>
            </template>
            <template #default="{ item }">
              <div class="item">
                <div class="content">
                  <div class="title">{{ item.name }}</div>
                  <div class="desc" />
                </div>
              </div>
            </template>
          </el-autocomplete>
          <el-button :icon="Search" type="primary" border @click="searchDetail" class="search-btn">搜索</el-button>
          <el-button type="default" border @click="router.go(-1)" class="cancel-btn">取消</el-button>
        </div>
      </div>
      <div class="header-decoration">
        <div class="decoration-circle circle-1"></div>
        <div class="decoration-circle circle-2"></div>
        <div class="decoration-circle circle-3"></div>
      </div>
    </div>
    <div class="search content container mx-auto max-w-screen-xl mt-5">
      <el-card class="search-card">
        <!-- 搜索历史 -->
        <div class="search-history">
          <div class="search-history-title">
            <div class="title-content">
              <el-icon class="title-icon"><Clock /></el-icon>
              <span>搜索历史</span>
            </div>
            <el-button circle :icon="Delete" size="small" type="danger" @click="clearHistory" class="clear-btn" />
          </div>
          <div class="search-history-list">
            <div class="search-history-item" v-for="(item, index) in historyStore.historyList" :key="index" @click="searchMusic(item)">
              <span class="history-text">{{ item }}</span>
              <el-icon @click.stop="removeHistory(item)" class="remove-icon">
                <CircleClose />
              </el-icon>
            </div>
          </div>
        </div>
        <!-- 热搜列表 -->
        <div class="hot-search">
          <div class="hot-search-title">
            <div class="title-content">
              <el-icon class="title-icon"><TrendCharts /></el-icon>
              <span>热搜列表</span>
            </div>
          </div>
          <div class="hot-search-list">
            <div
              class="hot-search-item"
              v-for="(item, index) in hotSearchList"
              :key="index"
              :class="{ hot: item.score > 40000 }"
              @click="searchMusic(item.searchWord)"
            >
              <div class="item-content">
                <span class="name">{{ item.searchWord }}</span>
                <span class="score">{{ item.score > 40000 ? "🔥" : "🌟" }}{{ item.score }}</span>
              </div>
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue"
import { getHotSearchAPI, getSearchSuggestAPI } from "@/api/web/music"
import { Search, Delete, Clock, TrendCharts } from "@element-plus/icons-vue"
import { useRouter } from "vue-router"
import { useHistoryStore } from "@/store/modules/history"
import { ElMessage } from "element-plus"
// 历史记录仓库
const historyStore = useHistoryStore()

const router = useRouter()

// 热搜列表
const hotSearchList = ref<any[]>([])

// 获取热搜列表
const getHotSearchList = async () => {
  const res = await getHotSearchAPI()
  if (res.code === 200) {
    console.log(res)
    hotSearchList.value = res.data
  }
}

// 当前搜索关键字
const searchWord = ref<string>("")

// 发送请求获取搜索建议
const getSearchSuggest = async (queryString: string, cb: (arg: any[]) => void) => {
  if (searchWord.value) {
    const res = await getSearchSuggestAPI(queryString)
    if (res.code === 200) {
      let data = [] as any[]
      res.result && Object.keys(res.result).length
        ? res.result.order.forEach((item: any) => {
            data.push(...res.result[item])
          })
        : (data = [{ name: "无数据" }])
      cb(data)
      return
    }
    cb([{ name: "无数据" }])
    return
  }
  cb([{ name: "无数据" }])
}

// 清除搜索框内容
const clearSearchWord = () => {
  searchWord.value = ""
}

// 选择搜索建议
const selectSuggest = (item: any) => {
  console.log(item)
  searchWord.value = item.name
  searchDetail()
}

const searchDetail = () => {
  if (searchWord.value) {
    historyStore.addHistory(searchWord.value)
    router.push(`/home-music/index/search-detail?keyword=${searchWord.value}`)
  } else {
    ElMessage.warning("请输入搜索内容")
  }
}

// 清空记录
const clearHistory = () => {
  historyStore.clearHistory()
}

// 清除记录
const removeHistory = (val: string) => {
  historyStore.removeHistory(val)
}

// 点击热搜
const searchMusic = (val: string) => {
  searchWord.value = val
  searchDetail()
}

onMounted(() => {
  getHotSearchList()
})
</script>

<style scoped lang="scss">
.search {
  .search-card {
    min-height: calc(100vh - 289px - 56px - 80px);
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.2);
    overflow: hidden;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);

    ::v-deep(.el-card__body) {
      padding: 0;
      background: transparent;
    }
  }

  .search-history {
    margin: 24px;
    min-height: 200px;

    .search-history-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 20px;

      .title-content {
        display: flex;
        align-items: center;
        gap: 12px;
        padding-left: 20px;
        font-size: 20px;
        font-weight: 700;
        border-left: 4px solid #409eff;
        border-radius: 2px;
        color: #303133;

        .title-icon {
          font-size: 22px;
          color: #409eff;
        }
      }

      .clear-btn {
        border-radius: 50%;
        transition: all 0.3s ease;

        &:hover {
          transform: scale(1.1);
        }
      }
    }

    .search-history-list {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      gap: 12px;

      .search-history-item {
        transition: all 0.3s ease;
        font-family: "楷体", "KaiTi", serif;
        padding: 8px 20px;
        border-radius: 20px;
        cursor: pointer;
        border: 1px solid #e4e7ed;
        display: flex;
        align-items: center;
        background: rgba(255, 255, 255, 0.8);
        backdrop-filter: blur(5px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

        .history-text {
          font-size: 14px;
          color: #606266;
        }

        .remove-icon {
          margin-left: 8px;
          color: #909399;
          font-size: 14px;
          transition: all 0.3s ease;

          &:hover {
            color: #f56c6c;
            transform: scale(1.1);
          }
        }

        &:hover {
          background: var(--gradient-music-primary);
          color: #fff;
          border-color: transparent;
          transform: translateY(-2px);
          box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);

          .history-text {
            color: #fff;
          }

          .remove-icon {
            color: rgba(255, 255, 255, 0.8);
          }
        }
      }
    }
  }

  .hot-search {
    margin: 24px;

    .hot-search-title {
      margin-bottom: 20px;

      .title-content {
        display: flex;
        align-items: center;
        gap: 12px;
        padding-left: 20px;
        font-size: 20px;
        font-weight: 700;
        border-left: 4px solid #409eff;
        border-radius: 2px;
        color: #303133;

        .title-icon {
          font-size: 22px;
          color: #409eff;
        }
      }
    }

    .hot-search-list {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
      gap: 16px;

      .hot-search-item {
        display: flex;
        align-items: center;
        padding: 16px 20px;
        cursor: pointer;
        border-radius: 16px;
        transition: all 0.3s ease;
        background: rgba(255, 255, 255, 0.8);
        backdrop-filter: blur(5px);
        border: 1px solid #e4e7ed;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

        &.hot {
          color: #e60026;
          font-weight: 700;
          background: var(--gradient-music-red);
          border-color: transparent;

          .name, .score {
            color: #fff;
          }
        }

        .item-content {
          display: flex;
          align-items: center;
          justify-content: space-between;
          width: 100%;

          .name {
            flex: 1;
            margin-left: 16px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            font-family: "楷体", "KaiTi", serif;
            color: #303133;
            transition: all 0.3s ease;
          }

          .score {
            margin-right: 16px;
            font-size: 14px;
            font-weight: 500;
            color: #909399;
            transition: all 0.3s ease;
          }
        }

        &:hover {
          background: var(--gradient-music-primary);
          transform: translateY(-4px);
          box-shadow: 0 8px 25px rgba(102, 126, 234, 0.25);
          border-color: transparent;

          .item-content {
            .name, .score {
              color: #fff;
            }
          }

          &.hot {
            background: var(--gradient-music-red-hot);
            transform: translateY(-4px) scale(1.02);
          }
        }
      }
    }
  }
}

.header {
  height: 80px;
  width: 100%;
  background: var(--gradient-music-primary);
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);

  .header-content {
    width: 100%;
    max-width: 1280px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    position: relative;
    z-index: 2;

    .header-left {
      display: flex;
      flex-direction: column;
      gap: 4px;

      .title {
        padding: 0;
        margin: 0;
        font-family: "楷体", "KaiTi", serif;
        font-size: 28px;
        font-weight: 700;
        color: #fff;
        text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
        letter-spacing: 2px;
      }

      .subtitle {
        font-size: 14px;
        color: rgba(255, 255, 255, 0.8);
        font-weight: 400;
        letter-spacing: 1px;
      }
    }

    .search {
      display: flex;
      align-items: center;
      gap: 12px;

      .search-input {
        width: 400px;

        ::v-deep(.el-input__wrapper) {
          border-radius: 25px;
          background: rgba(255, 255, 255, 0.95);
          backdrop-filter: blur(10px);
          border: 2px solid rgba(255, 255, 255, 0.3);
          box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
          transition: all 0.3s ease;
          padding: 0 20px;

          &:hover {
            background: rgba(255, 255, 255, 1);
            border-color: rgba(255, 255, 255, 0.5);
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
            transform: translateY(-2px);
          }

          &:focus-within {
            background: rgba(255, 255, 255, 1);
            border-color: #409eff;
            box-shadow: 0 8px 25px rgba(64, 158, 255, 0.3);
          }

          .el-input__prefix {
            color: #409eff;
            font-size: 18px;
            margin-right: 8px;
          }

          .el-input__inner {
            color: #303133;
            font-size: 14px;
            font-weight: 500;
            height: 44px;

            &::placeholder {
              color: #909399;
              font-weight: 400;
            }
          }
        }
      }

      .search-btn, .cancel-btn {
        height: 44px;
        border-radius: 22px;
        font-weight: 600;
        transition: all 0.3s ease;
        border: 2px solid transparent;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
        }
      }

      .search-btn {
        background: var(--gradient-music-blue);
        border-color: #409eff;

        &:hover {
          background: var(--gradient-music-blue-dark);
          border-color: #36a3f7;
        }
      }

      .cancel-btn {
        background: rgba(255, 255, 255, 0.9);
        color: #606266;
        border-color: rgba(255, 255, 255, 0.5);

        &:hover {
          background: rgba(255, 255, 255, 1);
          color: #303133;
          border-color: rgba(255, 255, 255, 0.8);
        }
      }
    }
  }

  .header-decoration {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;

    .decoration-circle {
      position: absolute;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.1);
      backdrop-filter: blur(5px);

      &.circle-1 {
        width: 120px;
        height: 120px;
        top: -60px;
        right: 10%;
        animation: float 6s ease-in-out infinite;
      }

      &.circle-2 {
        width: 80px;
        height: 80px;
        bottom: -40px;
        left: 15%;
        animation: float 8s ease-in-out infinite reverse;
      }

      &.circle-3 {
        width: 60px;
        height: 60px;
        top: 20px;
        left: 5%;
        animation: float 7s ease-in-out infinite;
      }
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .header {
    height: 70px;

    .header-content {
      padding: 0 16px;

      .header-left {
        .title {
          font-size: 24px;
        }

        .subtitle {
          font-size: 12px;
        }
      }

      .search {
        gap: 8px;

        .search-input {
          width: 280px;
        }

        .search-btn, .cancel-btn {
          height: 40px;
          border-radius: 20px;
          font-size: 12px;
          padding: 0 16px;
        }
      }
    }
  }

  .search {
    .search-card {
      margin: 0 16px;
    }

    .search-history, .hot-search {
      margin: 16px;

      .search-history-title, .hot-search-title {
        .title-content {
          font-size: 18px;

          .title-icon {
            font-size: 20px;
          }
        }
      }
    }

    .hot-search {
      .hot-search-list {
        grid-template-columns: 1fr;
        gap: 12px;
      }
    }
  }
}
</style>
